package com.huazi.user.registration.servlet;

import com.huazi.user.registration.entity.Ct_user;
import com.huazi.user.registration.entity.DiseaseInfo;
import com.huazi.user.registration.entity.User;
import com.huazi.user.registration.service.Imp.LoginService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.text.html.parser.Parser;
import java.io.IOException;
import java.util.List;

/**
 * 登录验证的servlet
 */
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        LoginService service = new LoginService();
        String account = req.getParameter("account");//获得账号
        String pwd = req.getParameter("pwd");//获得密码
        req.setAttribute("account", account);
        req.setAttribute("pwd", pwd);//保存起来
        boolean judge = service.login(pwd, account);//查看密码是否匹配成功
        if (judge) {
            //登陆成功后查询用户的信息 并且保存到会话中
            User user = service.selectUser(account);
            HttpSession session = req.getSession();
            session.setAttribute("user", user);//保存用户信息
            List<DiseaseInfo> list = service.selectDis(Integer.parseInt(account));
            session.setAttribute("list", list);//保存病例信息
            List<Ct_user> ct = service.selectCt(account);
            session.setAttribute("ct", ct);//保存检查报告
            req.getRequestDispatcher("Wang/userInfo/user.jsp").forward(req, resp);
            //转发到指定页面
        } else {
            req.setAttribute("error", "error");
            req.getRequestDispatcher("Wang/login/welcome.jsp").forward(req, resp);
            //没登陆成功重新登录
        }
    }
}
